
************************************************
*** Table on provisions and ETRs	     ***
************************************************

*Read
u "$cur_sample" if inlist(year,2016,2019), clear
do $cleaning/renameSOI.do 

*Keep ETR firms 	
keep if pidom_soi>0

*Set panel 
gegen t = group(year)
tsset firmid t

*ETR 
g etr = uscetr_soi*100 
g letr = ln(1 + etr)
g detr = d.etr
g dletr = d.letr

*Keep only 2016 + the ETR change 
foreach i in detr dletr {
	gegen x = max(`i'), by(firmid)
	replace `i' = x 
	drop x
}
keep if year==2016

*Reconstruct a couple variables
g etr_post = etr+detr
g payroll = cost_of_labor + salaries_wages_nde + compensation_officers

*Winsorize ETR change 
winsor2 detr, cuts(5 95) replace
winsor2 etr, cuts(0 95) replace
winsor2 etr_post, cuts(0 95) replace

/*******************************************************************************
Begin Summary stats table
*******************************************************************************/
est clear

*MTR brackets
g brk0 = taxable_income==0
g brk1 = taxable_income>0 & taxable_income<5e4
g brk2 = taxable_income>=5e4 & taxable_income<7.5e4
g brk3 = taxable_income>=7.5e4 & taxable_income<10e4
g brk4 = taxable_income>=1e5 & taxable_income<3.35e5
g brk5 = taxable_income>=.335e6 & taxable_income<10e6
g brk6 = taxable_income>=10e6 & taxable_income<15e6
g brk7 = taxable_income>=15e6 & taxable_income<18e6
g brk8 = taxable_income>=18e6 & !mi(taxable_income)

g brcat = 1 if brk0==1 & net_incm>0 & !mi(net_incm)
	replace brcat = 2 if brk0==1 & brcat!=1
	replace brcat = 3 if brk1==1
	replace brcat = 4 if brk2==1
	replace brcat = 5 if taxable_income>=7.5e4 & !mi(taxable_income)

	
global SUMM sale total_assets depreciable_assets payroll net_income tax

foreach v of varlist $SUMM {
	replace `v'=`v'/1e3
}

*Recreate variable labels
label var sale "Gross Receipts (000s)"
label var total_assets "Total Assets (000s)"
label var depreciable_assets "Depreciable Assets (000s)"
label var payroll "Payroll (000s)"
label var net_income "Net Income (000s)"
label var tax "Tax Paid (000s)"
label var etr "ETR in 2016"
label var etr_post "ETR in 2019"

preserve
	keep if year==2016
	
	gegen nfirms = total(weightSOI), by(brcat)
	label var nfirms "Unique Firms"
	
	gcollapse $SUMM emp_w2 etr etr_post nfirms [aw=weightSOI], by(brcat)
	format $SUMM emp_w2 nfirms %10.0fc
	format etr* %9.1f
	tostring *, force replace u
	sxpose, force clear firstnames 

	g row = ""
	replace row = "Gross Receipts (000s)" in 1 
	replace row = "Total Assets (000s)" in 2 
	replace row = "Depreciable Assets (000s)" in 3 
	replace row = "Payroll (000s)" in 4 
	replace row = "Net Income (000s)" in 5 
	replace row = "Tax Paid (000s)" in 6 
	replace row = "Employment" in 7 
	replace row = "ETR in 2016" in 8 
	replace row = "ETR in 2019" in 9 
	replace row = "Unique Firms" in 10

	*Format
	order row *
	replace _var5 = _var5 + " \\" if _n<10

	*Export to TeX
	listtex * using "$results/bracket_sumstats.tex", replace ///
		rstyle(tabular) ///
		head("\begin{tabular}{l c c c c c}" /// number of L or R justified columns in the table
		"\toprule" /// thick line at top of the table
		"           & (1)   	& (2)           	& (3)      	& (4)           & (5)  \\" ///
		"  & 0\% Bracket 	& 0\% Bracket 		& 15\%   	& 25\%  	& Above 25\%  \\" ///
		"  & Net Income $>=$ 0 	&  Net Income $<$ 0	& Bracket 	& Bracket	& Bracket \\ \midrule") /// 
			foot(   "\bottomrule" /// /// thick line at bottom of the table 
		"\end{tabular}")
restore


/*******************************************************************************
Summary Stats shares table by firmtype_alt
*******************************************************************************/ 
preserve
	keep if year==2016
	g n = 1 
	replace weightSOI = round(weightSOI)
	foreach v in n sale tax emp_w2 depreciable_assets{
		gegen `v'_tot = sum(`v') [fw=weightSOI]
		gegen `v'_type = sum(`v') [fw=weightSOI], by(brcat)
		g `v'_share = `v'_type / `v'_tot
	}
	collapse n_share sale_share tax_share emp_w2_share depreciable_assets_share, by(brcat)
	xpose, clear varname 
	drop in 1 
	g row = "\# Firms" if _varname=="n_share"
	replace row = "Gross Receipts" if _varname=="sale_share"
	replace row = "Tax Paid" if _varname=="tax_share"
	replace row = "Employment" if _varname=="emp_w2_share" 
	replace row = "Depreciable Assets" if _varname=="dprcbl_assts_share" 

	drop _varname 
	order row v* 
	format v* %9.3f
	tostring v*, force replace u
	replace v5 = v5 + " \\" if _n<5

	*Export to TeX
	listtex * using "$results/bracket_aggregates.tex", replace ///
		rstyle(tabular) ///
		head("\begin{tabular}{l c c c c c}" /// number of L or R justified columns in the table
		"\toprule" /// thick line at top of the table
		"               & (1)    & (2)    & (3)     & (4) & (5) \\" ///
			"Share of   	& 0\% Bracket 		& 0\% Bracket 		& 15\%   	& 25\%  	& Above 25\%  \\" ///
			"C Corp		& Net Income $>=$ 0 	&  Net Income $<$ 0	& Bracket 	& Bracket	& Bracket \\ \midrule") /// 
			foot(   "\bottomrule" /// /// thick line at bottom of the table
			"\end{tabular}")
restore
